#include "stdafx.h"
#include "TestObjectRegister.h"
#include "DatabaseBase.h"
#include "ObjectRegister.h"
#include "UeConnectionManager.h"

using namespace PDMS::PDMSDBIF;

CPPUNIT_TEST_SUITE_REGISTRATION( TestObjectRegister );


TestObjectRegister::TestObjectRegister()
{
}


TestObjectRegister::~TestObjectRegister()
{
}

void TestObjectRegister::TestInsert()
{
	DatabaseBase base( GET_REGISTER( DatabaseElementFactory, Design_VersionOne ) );
	base.DataSource( _T("D:\\UELIB\\libd\\DesignDBTemplate.mdb") );
	UEConnectionPtr ptr = base.DataSource();
	
	ObjectRegister* pReg = base.GetObjectRegister();
	ASSERT( pReg );

	UESoft::ErrorStatus es = pReg->Open();
	ASSERT( es == UESoft::eOk );

	pReg->NewRecord();
	pReg->SetID(1);
	pReg->SetClassName( _T("Test1") );
	pReg->SetObjectName( _T("Test1") );
	
	pReg->NewRecord();
	pReg->SetID(2);
	pReg->SetClassName( _T("Test2") );
	pReg->SetObjectName( _T("Test2") );

//	pReg->DeleteRecord( 1 );
//	pReg->DeleteRecord( 2 );

	pReg->Update();

	pReg->Close();
	CPPUNIT_ASSERT( ptr != NULL );
	UEConnectionManager::Instance().CloseAllConnection();
}


void TestObjectRegister::TestUpdate()
{
	DatabaseBase base( GET_REGISTER( DatabaseElementFactory, Design_VersionOne ) );
	base.DataSource( _T("D:\\UELIB\\libd\\DesignDBTemplate.mdb") );
	UEConnectionPtr ptr = base.DataSource();
	
	ObjectRegister* pReg = base.GetObjectRegister();
	ASSERT( pReg );
	
	UESoft::ErrorStatus es = pReg->Open();
	ASSERT( es == UESoft::eOk );
	
	pReg->UpdateRecord(1);
	pReg->SetID(1);
	pReg->SetClassName( _T("3") );
	pReg->SetObjectName( _T("3") );
	
	pReg->UpdateRecord(2);
	pReg->SetID(2);
	pReg->SetClassName( _T("4") );
	pReg->SetObjectName( _T("5") );
	
	pReg->Update();
	
	pReg->Close();
	CPPUNIT_ASSERT( ptr != NULL );
	UEConnectionManager::Instance().CloseAllConnection();
}

void TestObjectRegister::TestDelete()
{
	DatabaseBase base( GET_REGISTER( DatabaseElementFactory, Design_VersionOne ) );
	base.DataSource( _T("D:\\UELIB\\libd\\DesignDBTemplate.mdb") );
	UEConnectionPtr ptr = base.DataSource();
	
	ObjectRegister* pReg = base.GetObjectRegister();
	ASSERT( pReg );
	
	UESoft::ErrorStatus es = pReg->Open();
	ASSERT( es == UESoft::eOk );
	
	pReg->DeleteRecord( 1 );
	pReg->DeleteRecord( 2 );
	
	pReg->Update();
	
	pReg->Close();
	CPPUNIT_ASSERT( ptr != NULL );
	UEConnectionManager::Instance().CloseAllConnection();
}